package com.masterclient.app.modules.shop.contract;

import android.widget.Toast;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.common.bean.base.CommonBaseBean;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.shop.beans.StoreClassifyBean;
import com.eoner.baselibrary.utils.Utils;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/08/09
*/
public class StoreClassifyPresenter extends StoreClassifyContract.Presenter{

    @Override
    public void getStoreClassify( String id) {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().storeClassift(id), new Consumer<CommonBaseResponse<StoreClassifyBean>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<StoreClassifyBean> bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showData(bean.getData());
                }else {
                    mView.showDataFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                mView.showDataFailed("网络异常，请重试");
                super.accept(o);
            }
        }));
    }

    @Override
    public void collectShop(String id) {
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().addFavorite(String.valueOf(Utils.getTimeStamp()),id,"6"), new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.collectSuccess(true);
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        }));
    }

    @Override
    public void removeCollect(String id) {
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().removeFavorite(String.valueOf(Utils.getTimeStamp()),id,"6"), new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.collectSuccess(false);
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        }));
    }

}